{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import argparse\n",
    "import cv2\n",
    "import os\n",
    "import numpy as np\n",
    "from tqdm import tqdm\n",
    "import re\n",
    "import imutils\n",
    "from io import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def write_next_img(filename, txtname):\n",
    "    f = open(txtname, \"a\")\n",
    "    f.write(\"\\n\"+filename+\" \")\n",
    "    f.close()\n",
    "    \n",
    "def write_rect(rect):\n",
    "    global TXT\n",
    "    f = open(TXT, \"a\")\n",
    "    f.write(np.array_str(rect)+\" \")\n",
    "    f.close()\n",
    "\n",
    "def click_and_crop(event, x, y, flags, param):\n",
    "    global rect, tip_x, tip_y, clone\n",
    "    h,w,_ = clone.shape\n",
    "    if event == cv2.EVENT_LBUTTONDOWN:\n",
    "        rect=np.concatenate((rect,[x,y]))\n",
    "        if len(rect)==4:\n",
    "            write_rect(rect)\n",
    "            rect=np.array([])\n",
    "    elif event == cv2.EVENT_MOUSEMOVE:\n",
    "        white=(255,255,255)\n",
    "        clone  = image.copy()\n",
    "        if len(rect)==2:\n",
    "            cv2.rectangle(clone, tuple(map(int,rect)), (x,y), (0,255,0))\n",
    "        cv2.line(clone, (0,y),(w,y),white)\n",
    "        cv2.line(clone, (x,0),(x,h),white)\n",
    "        cv2.imshow(\"image\", clone)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Set scene, get paths and read resolution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "SCENE = \"blue_4\"\n",
    "\n",
    "DIR = get_scene_dir_path(SCENE)\n",
    "TXT = get_scene_labels_path(SCENE)\n",
    "\n",
    "_,_,_,RESOLUTION,LABEL_RESOLUTION=read_info(DIR)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Label"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      "  0%|          | 0/87 [00:00<?, ?it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0001.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      "  1%|          | 1/87 [00:01<01:43,  1.20s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0002.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      "  2%|▏         | 2/87 [00:01<01:19,  1.06it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0003.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  5%|▍         | 4/87 [00:01<00:46,  1.79it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0004.jpg\n",
      "0005.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  7%|▋         | 6/87 [00:04<01:02,  1.30it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0006.jpg\n",
      "0007.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  9%|▉         | 8/87 [00:04<00:34,  2.27it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0008.jpg\n",
      "0009.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 11%|█▏        | 10/87 [00:04<00:22,  3.49it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0010.jpg\n",
      "0011.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 14%|█▍        | 12/87 [00:04<00:15,  4.98it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0012.jpg\n",
      "0013.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 16%|█▌        | 14/87 [00:05<00:11,  6.27it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0014.jpg\n",
      "0015.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 18%|█▊        | 16/87 [00:05<00:09,  7.62it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0016.jpg\n",
      "0017.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 24%|██▍       | 21/87 [00:06<00:11,  5.61it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0018.jpg\n",
      "0019.jpg\n",
      "0020.jpg\n",
      "0021.jpg\n",
      "0022.jpg\n",
      "0023.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 26%|██▋       | 23/87 [00:06<00:15,  4.26it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0024.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 33%|███▎      | 29/87 [00:07<00:10,  5.57it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0025.jpg\n",
      "0026.jpg\n",
      "0027.jpg\n",
      "0028.jpg\n",
      "0029.jpg\n",
      "0030.jpg\n",
      "0031.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 43%|████▎     | 37/87 [00:07<00:05,  9.66it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0032.jpg\n",
      "0033.jpg\n",
      "0034.jpg\n",
      "0035.jpg\n",
      "0036.jpg\n",
      "0037.jpg\n",
      "0038.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 47%|████▋     | 41/87 [00:07<00:03, 12.23it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0039.jpg\n",
      "0040.jpg\n",
      "0041.jpg\n",
      "0042.jpg\n",
      "0043.jpg\n",
      "0044.jpg\n",
      "0045.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 56%|█████▋    | 49/87 [00:08<00:02, 18.00it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0046.jpg\n",
      "0047.jpg\n",
      "0048.jpg\n",
      "0049.jpg\n",
      "0050.jpg\n",
      "0051.jpg\n",
      "0052.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 66%|██████▌   | 57/87 [00:08<00:01, 23.47it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0053.jpg\n",
      "0054.jpg\n",
      "0055.jpg\n",
      "0056.jpg\n",
      "0057.jpg\n",
      "0058.jpg\n",
      "0059.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 75%|███████▍  | 65/87 [00:08<00:00, 27.58it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0060.jpg\n",
      "0061.jpg\n",
      "0062.jpg\n",
      "0063.jpg\n",
      "0064.jpg\n",
      "0065.jpg\n",
      "0066.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\r",
      " 79%|███████▉  | 69/87 [00:08<00:00, 28.85it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0067.jpg\n",
      "0068.jpg\n",
      "0069.jpg\n",
      "0070.jpg\n",
      "0071.jpg\n",
      "0072.jpg\n",
      "0073.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 89%|████████▊ | 77/87 [00:08<00:00, 30.92it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0074.jpg\n",
      "0075.jpg\n",
      "0093.jpg\n",
      "0094.jpg\n",
      "0095.jpg\n",
      "0096.jpg\n",
      "0097.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 98%|█████████▊| 85/87 [00:09<00:00, 31.90it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0098.jpg\n",
      "0099.jpg\n",
      "0100.jpg\n",
      "0101.jpg\n",
      "0102.jpg\n",
      "0103.jpg\n",
      "0104.jpg\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 87/87 [00:09<00:00,  9.46it/s]\n"
     ]
    }
   ],
   "source": [
    "filenames = sorted(os.listdir(DIR))\n",
    "filenames=[f for f in filenames if f != \"info.txt\"]\n",
    "#filenames=[\"r_2_99.jpg\"]\n",
    "cv2.namedWindow(\"image\")\n",
    "cv2.setMouseCallback(\"image\", click_and_crop)\n",
    "\n",
    "#variables\n",
    "rect = np.array([])\n",
    "tip_x, tip_y = (0,0)\n",
    "\n",
    "for i in tqdm(range(len(filenames))):\n",
    "    filename=filenames[i]\n",
    "    print(filename)\n",
    "    path=DIR+filename\n",
    "    image = cv2.imread(path)\n",
    "    #image = imutils.rotate(image, 1.5)\n",
    "    if RESOLUTION!=LABEL_RESOLUTION:\n",
    "        image = cv2.resize(image, LABEL_RESOLUTION)\n",
    "    clone = image.copy()\n",
    "    write_next_img(filename, TXT)\n",
    "    cv2.imshow(\"image\", clone)\n",
    "    cv2.waitKey(0)\n",
    "    rect = np.array([])\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Display labelled"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels = read_labels(SCENE)\n",
    "cv2.namedWindow(\"image\")\n",
    "for label in labels:\n",
    "    filename,point_list=label\n",
    "    path=DIR+filename\n",
    "    image = cv2.imread(path)\n",
    "    if RESOLUTION!=LABEL_RESOLUTION:\n",
    "        image = cv2.resize(image, LABEL_RESOLUTION) \n",
    "    for points in point_list:\n",
    "        p1,p2=points\n",
    "        cv2.rectangle(image, p1, p2, (0,255,0))\n",
    "    cv2.imshow(\"image\", image)\n",
    "    cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
